using System;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    class ProductionTestExtension : Production {
        private readonly ParseResult Result;
        private readonly Action Callback;
        public int NumberOfTimesParsed { get; private set; }

        public ProductionTestExtension (ParseResult result) : this (result, () => { }) {}

        public ProductionTestExtension (ParseResult result, Action callback) {
            NumberOfTimesParsed = 0;
            Result = result;
            Callback = callback;
        }

        protected override ParseResult ReallyParse (char[] source, int index, ParseContext context, string sourceString, ParseEnvironment parseEnvironment) {
            Callback ();
            NumberOfTimesParsed++;
            return Result;
        }
    }
}